package com.work.user.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.work.user.dao.MenuMapper;
import com.work.user.dao.RoleMapper;
import com.work.user.dao.UserMapper;
import com.work.user.pojo.Menu;
import com.work.user.pojo.Role;
import com.work.user.pojo.User;
import com.work.user.service.UserService;
import com.work.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;

    //查询所有用户
    @Override
    public List<User> findAll() {
        //查询所有用户
        List<User> users = userMapper.findAll();
        /*//遍历
        for (User user : users) {
            //获取用户ID
            Integer uid = user.getId();
            //获取角色
            Set<Role> roles = roleMapper.findByUserId(uid);
            //遍历角色获取角色id
            for (Role role : roles) {
                //获取角色对应权限
                Set<Menu> menus = menuMapper.findByRoleId(role.getId());
                role.setMenus(menus);
            }
            user.setRoles(roles);
        }*/
        return users;
    }

    //多条件搜索
    @Override
    public List<User> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        List<User> users = userMapper.selectByExample(example);
        /*//遍历
        for (User user : users) {
            //获取用户ID
            Integer uid = user.getId();
            //获取角色
            Set<Role> roles = roleMapper.findByUserId(uid);
            //遍历角色获取角色id
            for (Role role : roles) {
                //获取角色对应权限
                Set<Menu> menus = menuMapper.findByRoleId(role.getId());
                role.setMenus(menus);
            }
            user.setRoles(roles);
        }*/
        return users;
    }

    //条件+分页查询
    @Override
    public Page<User> findPage(int page, int size){
        PageHelper.startPage(page,size);
        return (Page<User>)this.findAll();
    }

    //多条件分页查询
    @Override
    public Page<User> findPage(Map<String,Object> searchMap, int page, int size){
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        List<User> users = userMapper.selectByExample(example);
        /*//遍历
        for (User user : users) {
            //获取用户ID
            Integer uid = user.getId();
            //获取角色
            Set<Role> roles = roleMapper.findByUserId(uid);
            //遍历角色获取角色id
            for (Role role : roles) {
                //获取角色对应权限
                Set<Menu> menus = menuMapper.findByRoleId(role.getId());
                role.setMenus(menus);
            }
            user.setRoles(roles);
        }*/
        return (Page<User>) users;
    }

    //添加用户
    @Override
    public void add(User user, Integer[] rids) {
        //添加用户
        userMapper.add(user);
        //获取用户id
        Integer uid = user.getId();
        //添加用户对应的角色id到中间表
        this.setUserIdAndRoleIds(uid, rids);
    }

    //修改用户
    @Override
    public void update(User user, Integer[] rids) {
        //修改用户
        userMapper.updateByPrimaryKey(user);
        //获取id
        Integer id = user.getId();
        //删除对应的中间表
        userMapper.deleteAssocication(id);
        //重新添加中间关系表
        this.setUserIdAndRoleIds(id, rids);
    }

    //物理删除用户
    @Override
    public void delete(Integer id) {
        //删除中间关系表
        userMapper.deleteAssocication(id);
        //删除用户
        userMapper.deleteByPrimaryKey(id);
    }

    //逻辑删除用户
    @Override
    public void updateStatus(User user) {
        userMapper.updateStatus(user);
    }

    @Override
    public User findById(Integer id) {
        User user = userMapper.findById(id);
        //获取用户ID
        Integer uid = user.getId();
        //获取角色
        Set<Role> roles = roleMapper.findByUserId(uid);
        /*//遍历角色获取角色id
        for (Role role : roles) {
            //获取角色对应权限
            Set<Menu> menus = menuMapper.findByRoleId(role.getId());
            role.setMenus(menus);
        }*/
        user.setRoles(roles);
        return user;
    }

    /*@Override
    public String findByPassword(String username) {
        return userMapper.findByPassword(username);
    }*/

    //修改密码
    @Override
    public void updatePassword(User user) {
//        String password = user.getPassword();
//        String s = MD5Utils.md5(password);
//        user.setPassword(s);
        userMapper.updatePassword(user);
    }

    /*//查询中间表，根据用户id查看所拥有的角色
    @Override
    public List<Integer> findUserByRole(Integer id) {
        return userMapper.findUserIdsByRoleId(id);
    }*/

    //添加到关系中间表
    private void setUserIdAndRoleIds(Integer uid, Integer[] rids) {
        if (rids != null && rids.length > 0) {
            for (Integer rid : rids) {
                Map<String, Integer> map = new HashMap<>();
                map.put("uid", uid);
                map.put("rid", rid);
                userMapper.setUserAndRole(map);
            }
        }
    }

    //构建查询对象
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 用户名
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }
            // 用户名
            if(searchMap.get("username")!=null && !"".equals(searchMap.get("username"))){
                criteria.andLike("username","%"+searchMap.get("username")+"%");
            }
            // 注册邮箱
            if(searchMap.get("email")!=null && !"".equals(searchMap.get("email"))){
                criteria.andLike("email","%"+searchMap.get("email")+"%");
            }
            // 注册手机号
            if(searchMap.get("phone")!=null && !"".equals(searchMap.get("phone"))){
                criteria.andLike("phone","%"+searchMap.get("phone")+"%");
            }
            // 状态
            if(searchMap.get("status")!=null && !"".equals(searchMap.get("status"))){
                criteria.andEqualTo("status",searchMap.get("status"));
            }
        }
        return example;
    }

}
